<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>字典表</title>
    <% include("../../common/css.html"){} %>
    <style>
        #dictTypeTable + .layui-table-view .layui-table-tool-temp {
            padding-right: 0;
        }
        #dictTypeTable + .layui-table-view .layui-table-body tbody > tr td {
            cursor: pointer;
        }
        #dictTypeTable + .layui-table-view .layui-table-body tbody > tr.layui-table-click {
            background-color: #fff3e0;
        }
        #dictTypeTable + .layui-table-view .layui-table-body tbody > tr.layui-table-click td:last-child > div:before {
            position: absolute;
            right: 6px;
            content: "\e602";
            font-size: 12px;
            font-style: normal;
            font-family: layui-icon !important;
            -webkit-font-smoothing: antialiased;
            -moz-osx-font-smoothing: grayscale;
        }
    </style>
</head>

<body>
<% include("../../layout/loading.html"){} %>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md3">
            <div class="layui-card">
                <div class="layui-card-body" style="padding: 10px;">
                    <form class="layui-form toolbar">
                        <div class="layui-form-item">
                            <div class="layui-inline" style="max-width: 140px;">
                                <input name="dictName" class="layui-input" placeholder="输入字典名称" autocomplete="off"/>
                            </div>
                            <div class="layui-inline">
                                <button class="layui-btn icon-btn" lay-filter="dictTbSearch" lay-submit>
                                    <i class="layui-icon">&#xe615;</i>搜索
                                </button>
                            </div>
                        </div>
                    </form>
                    <!-- 字典类型 -->
                    <table id="dictTypeTable" lay-filter="dictTypeTable"></table>
                </div>
            </div>
        </div>
        <div class="layui-col-md9">
            <div class="layui-card">
                <div class="layui-card-body" style="padding: 10px;">
                    <form class="layui-form toolbar">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">字典名称:</label>
                                <div class="layui-input-inline">
                                    <input name="dictDataName" class="layui-input" placeholder="输入字典名称" autocomplete="off"/>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">字典码:</label>
                                <div class="layui-input-inline">
                                    <input name="dictDataCode" class="layui-input" placeholder="输入字典码" autocomplete="off"/>
                                </div>
                            </div>
                            <div class="layui-inline">&emsp;
                                <button class="layui-btn icon-btn" lay-filter="dictDataTbSearch" lay-submit>
                                    <i class="layui-icon">&#xe615;</i>搜索
                                </button>
                            </div>
                        </div>
                    </form>
                    <!-- 字典数据 -->
                    <table class="layui-table" id="dictDataTable" lay-filter="dictDataTable"></table>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 字典类型-表单弹窗 -->
<script type="text/html" id="dictTypeEditDialog">
    <form id="dictTypeEditForm" lay-filter="dictTypeEditForm" class="layui-form model-form">
        <input type="hidden" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">字典名称:</label>
            <div class="layui-input-block">
                <input name="dictName" placeholder="请输入字典名称" class="layui-input" autocomplete="off"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">字典码:</label>
            <div class="layui-input-block">
                <input name="dictCode" placeholder="请输入字典码" class="layui-input" autocomplete="off"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">排序:</label>
            <div class="layui-input-block">
                <input name="sort" placeholder="请输入排序" class="layui-input" type="number"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注:</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入备注" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="dictTypeEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>

<!-- 字典类型-表单弹窗 -->
<script type="text/html" id="dictDataEditDialog">
    <form id="dictDataEditForm" lay-filter="dictDataEditForm" class="layui-form model-form">
        <input type="hidden" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">字典名称:</label>
            <div class="layui-input-block">
                <input name="dictName" placeholder="请输入字典名称" class="layui-input" autocomplete="off"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">字典码:</label>
            <div class="layui-input-block">
                <input name="dictCode" placeholder="请输入字典码" class="layui-input" autocomplete="off"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">字典值:</label>
            <div class="layui-input-block">
                <input name="dictValue" placeholder="请输入字典值" class="layui-input" autocomplete="off"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">排序:</label>
            <div class="layui-input-block">
                <input name="sort" placeholder="请输入排序" class="layui-input" type="number"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注:</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入备注" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="dictDataEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>

<!-- 表格操作列 -->
<script type="text/html" id="tableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- js部分 -->
<% include("../../common/js.html"){} %>
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'zTree'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var admin = layui.admin;
        var selObj;  // 左表选中数据

        /* 渲染字典类型表格 */
        var insTb = table.render({
            elem: '#dictTypeTable',
            url: '/system/dict/dictTypes',
            height: 'full-100',
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['limit', 'count', 'prev', 'page', 'next'] //自定义分页布局
                , limit: 10
                , limits: [10, 15, 30, 50, 100, 200]
                , groups: 1 //只显示 1 个连续页码
                , first: false //不显示首页
                , last: false //不显示尾页
            },
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                '<button lay-event="edit" class="layui-btn layui-btn-sm layui-btn-warm icon-btn"><i class="layui-icon">&#xe642;</i>修改</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                '</p>'].join(''),
            defaultToolbar: [],
            cols: [[
                {field: 'dictName', title: '字典类型', templet:function (d) {
                        if (d.remark && d.remark != ''){
                            return "<span lay-tips='" + d.remark + "' lay-direction='2'>" + d.dictName + "</span>"
                        } else {
                            return d.dictName;
                        }
                    }},
                {field: 'dictCode', title: '字典码',width: 200,align: 'center'}
            ]],
            done: function (res, curr, count) {
                $('#dictTypeTable+.layui-table-view .layui-table-body tbody>tr:first').trigger('click');
            }
        });

        form.on('submit(dictTbSearch)', function (data) {
            insTb.reload({where: data.field});
            return false;
        });

        /* 监听字典类型表格行单击事件 */
        table.on('row(dictTypeTable)', function (obj) {
            selObj = obj;
            obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
            insTb2.reload({where: {pid: obj.data.id}, page: {curr: 1}});
        });

        table.on('toolbar(dictTypeTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showDictTypeModel();
            } else if (obj.event === 'edit') { // 修改
                showDictTypeModel(selObj.data, selObj);
            } else if (obj.event === 'del') { // 删除
                doDictTypeDel(selObj);
            }
        });

        //新增或修改字典类型
        function showDictTypeModel(mData, obj){
            admin.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '字典类型',
                content: $('#dictTypeEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('dictTypeEditForm', mData);
                    // 表单提交事件
                    form.on('submit(dictTypeEditSubmit)', function (data) {
                        var loadIndex = layer.load(2);
                        $.get(mData ? '/system/dict/updateDictType' : '/system/dict/addDictType', data.field, function (res) {
                            layer.close(loadIndex);
                            if (200 === res.code) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload();
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });
                }
            });
        }

        //删除字典类型
        function doDictTypeDel(mData){
            layer.confirm('确定要删除['+mData.data.dictName+']字典类型吗，该字典下的所有数据将被清除？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                $.get('/system/dict/deleteDictType', {
                    id: mData.data.id,
                }, function (res) {
                    layer.close(loadIndex);
                    if (200 === res.code) {
                        layer.msg(res.msg, {icon: 1});
                        mData.del();
                        $('#dictTypeTable+.layui-table-view .layui-table-body tbody>tr:first').trigger('click');
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }


        // 渲染字典项表格
        var insTb2 = table.render({
            elem: '#dictDataTable',
            url: '/system/dict/dictDatas',
            page: true,
            limit: 15,
            limits:[10,15,20,30,50,100,300],
            toolbar: ['<p>',
                '<button lay-event="addDict" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>新增字典项</button>&nbsp;',
                '<button lay-event="batchEnable" class="layui-btn layui-btn-sm layui-btn-warm icon-btn"><i class="layui-icon">&#xe609;</i>批量启用</button>&nbsp;',
                '<button lay-event="batchDisable" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#x1006;</i>批量停用</button>&nbsp;',
                '<button lay-event="batchDel" class="layui-btn layui-btn-sm layui-bg-black icon-btn"><i class="layui-icon">&#xe640;</i>批量删除</button>&nbsp;',
                '</p>'].join(''),
            defaultToolbar: [],
            height: 'full-100',
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox', fixed: 'left'},
                {
                    field: 'dictName', title: '字典项名称', minWidth: 170, templet: function (d) {
                        if (d.remark && d.remark != '') {
                            return "<span lay-tips='" + d.remark + "' lay-direction='2'>" + d.dictName + "</span>";
                        } else {
                            return d.dictName;
                        }
                    }
                },
                {field: 'dictCode', title: '字典项编码', minWidth: 200},
                {field: 'dictValue', title: '字典值',align: 'center', minWidth: 90, edit: 'text'},
                {field: 'sort', title: '排序',align: 'center', minWidth: 70, edit: 'text'},
                {field: 'status', title: '状态',minWidth: 110,align: 'center',templet:function (d) {
                        if (d.status == 1){
                            return "<span style='color: green;'>启用</span>";
                        } else if(d.status == 0){
                            return "<span style='color: red;'>停用</span>";
                        }
                    }},
                {field: 'createTime', title: '创建时间',align: 'center', minWidth: 90},
                {align: 'center', toolbar: '#tableBar', title: '操作', width: 120, fixed: 'right'}
            ]]
        });

        form.on('submit(dictDataTbSearch)', function (data) {
            insTb2.reload({where: data.field});
            return false;
        });

        table.on('toolbar(dictDataTable)', function (obj) {
            if (obj.event === 'addDict') { // 添加
                showDictDataModel();
            } else if (obj.event === 'batchEnable'){//批量启用
                var checkStatus = table.checkStatus('dictDataTable');
                if (checkStatus.data.length==0){
                    layer.msg('请勾选要启用的数据', {icon: 2});
                    return;
                }
                var ids = "";
                for ( var i = 0; i <checkStatus.data.length; i++){
                    ids += checkStatus.data[i].id+ ",";
                }
                ids = ids.substr(0, ids.length - 1);
                batchHandle(ids, 1);
            } else if (obj.event === 'batchDisable'){//批量停用
                var checkStatus = table.checkStatus('dictDataTable');
                if (checkStatus.data.length==0){
                    layer.msg('请勾选要停用的数据', {icon: 2});
                    return;
                }
                var ids = "";
                for ( var i = 0; i <checkStatus.data.length; i++){
                    ids += checkStatus.data[i].id+ ",";
                }
                ids = ids.substr(0, ids.length - 1);
                batchHandle(ids, 0);
            } else if (obj.event === 'batchDel') { //批量删除
                var checkStatus = table.checkStatus('dictDataTable');
                if (checkStatus.data.length==0){
                    layer.msg('请勾选要删除的数据', {icon: 2});
                    return;
                }
                var ids = "";
                for ( var i = 0; i <checkStatus.data.length; i++){
                    ids += checkStatus.data[i].id+ ",";
                }
                ids = ids.substr(0, ids.length - 1);
                batchHandle(ids, -1);
            }
        });

        table.on('tool(dictDataTable)', function(obj){
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                showDictDataModel(data);
            } else if (layEvent === 'del') { // 删除
                doDel2(data.id);
            }
        });

        //删除字典项
        function doDel2(id) {
            top.layer.confirm('确定要删除吗？', {
                skin: 'layui-layer-admin'
            }, function (i) {
                top.layer.close(i);
                layer.load(2);
                $.post('/system/dict/deleteDictData', {
                    id: id
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        insTb2.reload();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }

        //批量操作
        function batchHandle(ids, state){
            top.layer.confirm('确定要批量'+(state == 1?'启用':state==0?'停用':'删除')+'勾选数据吗？', {
                skin: 'layui-layer-admin'
            }, function (i) {
                top.layer.close(i);
                layer.load(2);
                $.post('/system/dict/batchHandleDictData', {
                    ids: ids,
                    state: state
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        insTb2.reload();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }

        function showDictDataModel(mData, obj){
            admin.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '字典项',
                content: $('#dictDataEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('dictDataEditForm', mData);
                    // 表单提交事件
                    form.on('submit(dictDataEditSubmit)', function (data) {
                        data.field.pid = selObj.data.id;
                        var loadIndex = layer.load(2);
                        $.get(mData ? '/system/dict/updateDictData' : '/system/dict/addDictData', data.field, function (res) {
                            layer.close(loadIndex);
                            if (200 === res.code) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                if (obj) {
                                    obj.update(data.field);
                                } else {
                                    insTb2.reload();
                                }
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });
                }
            });
        }

    });
</script>
</body>
</html>

